<template>
    <h3>日志文件管理</h3>
    <el-table :data="tableData" @cell-dblclick="$func.elTableCopyText">
        <!-- <el-table-column prop="id" label="文件编号"/> -->
        <el-table-column prop="name" label="名称"/>
        <el-table-column label="时间范围" width="320">
            <template #default="scope">
                <span v-if="scope.row.min_id == 0 || scope.row.max_id == 0">记录进行中</span>
                <span v-else>{{ $func.timesmarp(scope.row.min_id,3) }} ~ {{ $func.timesmarp(scope.row.max_id,3) }}</span>
            </template>
        </el-table-column>
        <el-table-column label="文件大小" width="120">
            <template #default="scope">
                {{ $func.formatBytesSize(scope.row.file_size) }}
            </template>
        </el-table-column>
        <el-table-column prop="path" label="文件位置" show-overflow-tooltip>
            <template #default="scope">
                <span v-if="scope.row.is_deleted == 0">{{ scope.row.path }}</span>
                <span v-if="scope.row.is_deleted != 0">源文件已删除，压缩文件位置：{{ scope.row.path_7z }}，恢复时请手动恢复到 {{ scope.row.path }} 后，打开数据库进行调整数据库中的文件位置</span>
            </template>
        </el-table-column>
        <el-table-column label="创建时间" width="170">
            <template #default="scope">
                {{ $func.timesmarp(scope.row.id,3) }}
            </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
            <template #default="scope">
                <el-button text @click="p7z(scope.row.id)" v-if="scope.row.path_7z == ''">压缩</el-button>
                <a :href="domain+'file.download?id='+scope.row.id" target="_blank" v-if="scope.row.path_7z != ''">下载</a>
                <el-button text @click="p7delete(scope.row.id)" v-if="scope.row.path_7z != '' && scope.row.is_deleted == 0">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination class="pages" background layout="prev, pager, next" :total="count" :page-size="pageLimit" v-model:current-page="page" @current-change="initData"/>
</template>
<script>
import config from '../../config/init.js'
import { ElLoading } from 'element-plus'
export default {
    data() {
        return {
            domain: config.domains.api,
            count:0,
            page:1,
            pageLimit:10,
            tableData: [],
        }
    },
    methods:{
        // 页面初始化
        initData(){
            var that = this
            that.$axios.get("api:files",{params:{
                page: that.page,
                limit: that.pageLimit,
            }})
            .then(res => {
                that.tableData = res.data
                that.page = res.pages.current_page
                if(that.count == 0){
                    that.count = res.total
                }
            })
        },
        // 文件压缩
        p7z(id){
            var that = this
            var loading = ElLoading.service({
                lock: true,
                text: '正在压缩...',
                background: 'rgba(0, 0, 0, 0.7)',
            })
            that.$axios.get("api:file.7z",{params:{
                id: id
            }})
            .then(() => {
                loading.close()
                that.$func.notify("压缩成功")
                that.initData()
            })
            .catch(()=>{
                loading.close()
            })
        },
        // 文件删除
        p7delete(id){
            var that = this
            that.$axios.get("api:file.delete",{params:{
                id: id
            }})
            .then(() => {
                that.$func.notify("删除成功")
                that.initData()
            })
        },
    },
    mounted() {
        this.initData()
    },
}
</script>